﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace UITLibraryManagement
{
    public interface IAuthenticationService
    {
        User AuthenticateUser(string username,string password);
    }

    public class AuthenticationService : IAuthenticationService
    {
        private class InternalUserData
        {
            public InternalUserData(string username, string email, string hashedPassword, string[] roles)
            {
                Username = username;
                Email = email;
                HashedPassword = hashedPassword;
                Roles = roles;
            }
            public string Username
            {
                get;
                private set;
            }
            public string Email
            {
                get;
                private set;
            }
            public string HashedPassword
            {
                get;
                private set;
            }
            public string[] Roles
            {
                get;
                private set;
            }
        }//end InternalUserData

        private static readonly List<InternalUserData> _users = new List<InternalUserData>()
        {
            new InternalUserData("phuc", "bluesea@glove.com",
                "utpAa1AkrxfMuQ/wZcnZvL65lLQX4D3iqSUYMzeXAA8=",new string[] { "Administrators" }),
            new InternalUserData("thanh", "binposible@bobo.com",
                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",new string[] {}),
            new InternalUserData("hieu", "mrkupikuto@huhu.com",
                "cd372fb85148700fa88095e3492d3f9f5beb43e555e5ff26d95f5a6adc36f8e6",new string[] {})
        };

        public User AuthenticateUser(string username, string clearTextPassword)
        {
            InternalUserData userData = _users.FirstOrDefault(u => u.Username.Equals(username) &&
                        u.HashedPassword.Equals(CalculateHash(clearTextPassword, u.Username)));
            if (userData == null)
                throw new UnauthorizedAccessException("Access denied. Please provide some valid credentials.");

            return new User(userData.Username, userData.Email, userData.Roles);
        }

        private string CalculateHash(string clearTextPassword, string salt)
        {
            // Convert the salted password to a byte array
            byte[] saltedHashBytes = Encoding.UTF8.GetBytes(clearTextPassword + salt);
            // Use the hash algorithm to calculate the hash
            HashAlgorithm algorithm = new SHA256Managed();
            byte[] hash = algorithm.ComputeHash(saltedHashBytes);
            // Return the hash as a base64 encoded string to be compared to the stored password
            string hashString =  Convert.ToBase64String(hash);
            return hashString;
        }
    }
}
/*
*Tham khảo: http://blog.magnusmontin.net/2013/03/24/custom-authorization-in-wpf/
*/
